package com.kabao.javafx.flip;

import javafx.animation.RotateTransition;
import javafx.animation.SequentialTransition;
import javafx.application.Application;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.Random;

/**
 * 扑克牌翻转
 */
public class PokerApp extends Application {
    Pane frontPane;

    @Override
    public void start(Stage primaryStage) throws Exception {

        frontPane = new Pane();
        //旋转轴
        frontPane.setRotationAxis(Rotate.Y_AXIS);
        frontPane.setVisible(true);
        frontPane.setStyle("-fx-background-color: #ccccff");

        Pane backPane = new Pane();
        String bgUrl = getClass().getResource("/img/poker/bg.png").toExternalForm();
//        backPane.setStyle("-fx-background-image: url('" + bgUrl+ "')");
        backPane.setStyle("-fx-background-image: url('" + bgUrl + "')");
        StackPane stackPane = new StackPane(frontPane, backPane);
        //根据poker图片大小设置
        stackPane.setMaxSize(163, 214);

        stackPane.setOnMouseClicked(mouseEvent -> {
            //小于中间，则点击的是左侧，否则点击的是右侧
            dir = mouseEvent.getX() <= stackPane.getWidth() / 2;
            if (backPane.isVisible()) {
                flipAnim(frontPane, backPane, dir);
            } else {
                flipAnim(backPane, frontPane, dir);
            }
        });
        Scene scene = new Scene(new BorderPane(stackPane), 500, 500);
        scene.setCamera(new PerspectiveCamera());
        primaryStage.setScene(scene);
        primaryStage.setTitle("反转");
        primaryStage.show();
    }

    private boolean fliping;
    private boolean dir;

    private void flipAnim(Pane showPane, Pane hidePane, boolean dir) {
        if (fliping) {
            return;
        }
        fliping = true;
        if (showPane == frontPane) {
            String pokerUrl = getClass().getResource("/img/poker/poker.png").toExternalForm();
            Random random = new Random();
            int px = random.nextInt(13);
            System.out.println("点数 = " + (px + 2));
            int x = - px * 163;
            int py = random.nextInt(4);
            System.out.println("花色 = " + py);
            int y = py * 214;


            frontPane.setStyle("-fx-background-image: url('" + pokerUrl + "');" + "-fx-background-position: " + x + " " + y + ";");
        }

        Duration duration = Duration.millis(500);
        //隐藏当前显示的页面
        RotateTransition hideRt = new RotateTransition(duration, hidePane);
        //反转轴
        hideRt.setAxis(Rotate.Y_AXIS);
        hideRt.setFromAngle(0);
        hideRt.setToAngle(dir ? 90 : -90);
        hideRt.setOnFinished(actionEvent -> {
            hidePane.setVisible(false);
            showPane.setVisible(true);
        });

        //显示刚刚隐藏的页面
        RotateTransition showRt = new RotateTransition(duration, showPane);
        showRt.setAxis(Rotate.Y_AXIS);
        showRt.setFromAngle(dir ? -90 : 90);
        showRt.setToAngle(0);


        SequentialTransition st = new SequentialTransition(hideRt, showRt);
        st.setOnFinished(actionEvent -> fliping = false);
        st.play();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
